package com.baizhi.service.impl;

import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDAO adminDAO;
    @Override
    public void login(Admin admin) {
        Admin admin1 = adminDAO.selectByName(admin.getUsername());
        if(admin1!=null){
            if(admin.getPassword().equals(admin1.getPassword())){
                System.out.println("service登录成功");
            }else{
                throw new RuntimeException("账户或密码输入错误");
            }
        }else {
            throw new RuntimeException("用户不存在");
        }
    }

    @Override
    public PageInfo<Map> selectAll(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<Map> admins = adminDAO.selectAll();
        PageInfo<Map> pageInfo = new PageInfo<>(admins);
        return pageInfo;
    }

    @Override
    public void insert(Admin admin) {
        adminDAO.insert(admin);
    }

    @Override
    public void updateByPrimaryKeySelective(Admin admin) {
        adminDAO.updateByPrimaryKeySelective(admin);

    }

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        Admin admin = adminDAO.selectByPrimaryKey(id);
        return admin;
    }

    @Override
    public void deleteByPrimaryKey(Integer id) {
        adminDAO.deleteByPrimaryKey(id);
    }

    @Override
    public void add(Integer adminId, Integer orderId) {
        adminDAO.add(adminId,orderId);
    }
}
